package com.reconcile.phone.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reconcile.phone.business.domain.RpPhoneCode;
import com.reconcile.phone.business.domain.bo.RpPhoneCodeBo;
import com.reconcile.phone.business.domain.vo.RpPhoneCodeVo;
import com.reconcile.phone.business.mapper.RpPhoneCodeMapper;
import com.reconcile.phone.business.service.RpPhoneCodeService;
import com.reconcile.phone.common.core.domain.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: liuliyou
 * Date: 2025/8/5
 */
@Service
@RequiredArgsConstructor
public class RpPhoneCodeServiceImpl implements RpPhoneCodeService {

    private final RpPhoneCodeMapper rpPhoneCodeMapper;

    /**
     * 新增手机号
     *
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPhoneCode(RpPhoneCodeBo bo) {
        //查询是否添加过
        RpPhoneCodeVo rpPhoneCodeVo = rpPhoneCodeMapper.selectVoOne(new LambdaQueryWrapper<RpPhoneCode>()
                .eq(RpPhoneCode::getPhoneNum, bo.getPhoneNum()));
        if (ObjectUtil.isNotNull(rpPhoneCodeVo)) {
            throw new RuntimeException("手机号【" + bo.getPhoneNum() + "】已存在！");
        }
        RpPhoneCode rpPhoneCode = BeanUtil.toBean(bo, RpPhoneCode.class);
        int result = rpPhoneCodeMapper.insert(rpPhoneCode);
        if (result > 0) {
            return result;
        } else {
            throw new RuntimeException("新增手机号失败！");
        }
    }

    /**
     * 编辑手机号
     *
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePhoneCode(RpPhoneCodeBo bo) {
        //查询是否添加过
        RpPhoneCodeVo rpPhoneCodeVo = rpPhoneCodeMapper.selectVoOne(new LambdaQueryWrapper<RpPhoneCode>()
                .eq(RpPhoneCode::getPhoneNum, bo.getPhoneNum()));
        if (ObjectUtil.isNotNull(rpPhoneCodeVo)) {
            throw new RuntimeException("手机号【" + bo.getPhoneNum() + "】已存在！");
        }
        RpPhoneCode rpPhoneCode = BeanUtil.toBean(bo, RpPhoneCode.class);
        int result = rpPhoneCodeMapper.updateById(rpPhoneCode);
        if (result > 0) {
            return result;
        } else {
            throw new RuntimeException("编辑手机号失败！");
        }
    }

    /**
     * 手机号列表查询
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public Page<RpPhoneCodeVo> selectPhoneCodeList(RpPhoneCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RpPhoneCode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(RpPhoneCode::getCreateTime);
        return rpPhoneCodeMapper.selectVoPage(pageQuery.build(), queryWrapper);
    }

    /**
     * 手机号详细信息
     *
     * @param phoneCodeId
     * @return
     */
    @Override
    public RpPhoneCodeVo queryPhoneCodeById(Long phoneCodeId) {
        return rpPhoneCodeMapper.selectVoById(phoneCodeId);
    }

    /**
     * 删除手机号
     *
     * @param phoneCodeId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePhoneCodeById(Long phoneCodeId) {
        return rpPhoneCodeMapper.deleteById(phoneCodeId);
    }
}
